const Koa = require('koa')
const { koaBody } = require('koa-body')
const cors = require('@koa/cors')
const Router = require('koa-Router')
const bodyParser = require('koa-bodyparser')
const app = new Koa()
const router = new Router()
const jwt = require('jsonwebtoken')
const { WebSocketServer } = require('ws');
const userdata = require('./login/user.json')
const Mock = require('mockjs')
//营销热点
const marketlist = require('./MarketingHotspots/list.json')
//聚焦近期热点
const focuslist = require('./FocusHot/list.json')
//AIGC智图
const aicglist = require('./AIGC/list.json')
// 3D视觉
const visionlist = require('./Vision/list.json')
//行业趋势-视频篇
const videolist = require('./Video/list.json')
//行业趋势—海报篇
const postlist = require('./PosterArticle/list.json')
// 行业趋势—实拍篇
const reallist = require('./RealShooting/list.json')
// 行业趋势—配乐篇
const musiclist = require('./MusicPiece/list.json')
// 行业趋势—人像篇
const portlist = require('./PortraitSection/list.json')
// 行业趋势—电商篇
const commlist = require('./Ecommerce/list.json')
// 专题页面
const special = require("./Special/list.json");
// 背景
const listbackground = require("./Background/list.json");
// 图片
const picture = require("./Picture/list.json");
//点击节日跳转
const templatecenter = require('./TemplateCenter/list.json')
//订单管理
const orderlist = require('./Order/list.json')
//模块商品管理
const moduleshop = require('./Shop/list.json')
const data = Mock.mock({
    'list|130': [{
        'num|+1': 1,
        "name": "@cname(2)",
        "title": "@ctitle(5)",
        "phone|1": ['18295727806', '18307792424', '18190855203', '18131796594', '15931769345', '13845633206'],
        "key": "@id",
        "type|1": ['充值', '订单消费', '退款'],
        "type1|1": ['支出', '收入'],
        "date": "@date()",
        "price|1000-5000": 1200
    }]
});
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
    ws.on('error', console.error);
    ws.on('message', function message(data) {
        console.log('received: %s', data);
        if (data.toString() == '你好') {
            ws.send('hello')
        } else if (data.toString() == '谢谢') {
            ws.send('欢迎下次光临')
        } else {
            ws.send(data.toString())
        }
    });
});
app.use(cors())
app.use(koaBody({ multipart: true }))
app.use(bodyParser())
app.use(router)
router.post('/api/login', ctx => {
    let { username, password } = ctx.request.body
    let flag = userdata.some(item => item.username == username || item.password === password)
    let obj = userdata.find(item => item.username == username || item.password === password)
    let token = jwt.sign({
        data: username
    }, 'secret', { expiresIn: 60 * 60 });

    if (flag) {
        ctx.body = {
            code: 200,
            msg: "登录成功",
            obj: obj,
            token: token
        }
    }
})
// 营销热点接口
router.get('/api/list', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: marketlist.list
    }
})
////聚焦近期热点接口
router.get('/api/list1', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: focuslist.list1
    }
})
//AIGC智图接口
router.get('/api/list2', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: aicglist.list2
    }
})
// 3D视觉接口
router.get('/api/list3', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: visionlist.list3
    }
})
//行业趋势-视频篇
router.get('/api/list4', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: videolist.list4
    }
})
//行业趋势—海报篇
router.get('/api/list5', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: postlist.list5
    }
})
// 行业趋势—实拍篇
router.get('/api/list6', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: reallist.list6
    }
})
// 行业趋势—配乐篇
router.get('/api/list7', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: musiclist.list7
    }
})
// 行业趋势—人像篇
router.get('/api/list8', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: portlist.list8
    }
})
// 行业趋势—电商篇
router.get('/api/list9', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: commlist.list9
    }
})
// 专题页面
router.get("/api/list10", (ctx) => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: special.list10,
    };
});
// 图片
router.get("/api/listpicture", (ctx) => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: picture.listpicture,
    };
});
// 背景
router.get("/api/listbackground", (ctx) => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: listbackground.listbackground,
    };
});
//点击节日跳转接口
router.get('/api/list12', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: templatecenter.list12
    };
})
//果币充值列表接口
router.get('/api/fruitlist', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: data.list
    }
})
//订单管理
router.get('/api/orderlist', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: orderlist.orderlist
    }
})
//模块商品管理
router.get('/api/shoplist', ctx => {
    ctx.body = {
        code: 200,
        msg: "获取成功",
        data: moduleshop.shoplist
    }
})
app.listen(3002, () => {
    console.log('服务启动成功')
})